package cn.itrip.trade.service;

import cn.itrip.dao.ItripHotelOrderMapper;
import cn.itrip.dao.ItripHotelRoomMapper;
import cn.itrip.dao.ItripHotelTempStoreMapper;
import cn.itrip.dao.ItripTradeEndsMapper;
import cn.itrip.pojo.ItripHotelOrder;
import cn.itrip.pojo.ItripTradeEnds;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by LENOVO on 2018/8/28.
 */
@Service
public class PayServiceImpl implements PayService {

    @Resource
    private ItripHotelOrderMapper itripHotelOrderMapper;

    @Resource
    private ItripTradeEndsMapper itripTradeEndsMapper;

    @Resource
    private ItripHotelTempStoreMapper itripHotelTempStoreMapper;

    /**
     *获取订单信息
     * @param orderNo
     * @return
     * @throws Exception
     */
    @Override
    public ItripHotelOrder getHotelOrder(String orderNo) throws Exception {
        return itripHotelOrderMapper.selectByOrderNo(orderNo);
    }

    /**
     * 修改訂單，并寫如微信訂單 交易號
     * @param orderNo
     * @param status
     * @param transactionId
     * @return
     */
    @Override
    public Boolean modifierHotelOrder(String orderNo, Integer status,String transactionId) {

        return  itripHotelOrderMapper.updateByPrimaryKeyAndStatus(Long.parseLong(orderNo),status,transactionId)>0;
    }

    /**
     * 支付成功 修改订单
     * @param out_trade_no
     * @param Paytype
     * @param trade_no
     * @return
     * @throws Exception
     */
    @Override
    public void paySuccess(String out_trade_no, Integer Paytype, String trade_no) throws Exception {
        ItripHotelOrder itripHotelOrder = new ItripHotelOrder();
        itripHotelOrder.setOrderno(out_trade_no);
        itripHotelOrder.setOrderstatus(2);
        itripHotelOrder.setPaytype(Paytype);
        itripHotelOrder.setTradeno(trade_no);
        //更新支付状态
        itripHotelOrderMapper.updateOrderByPay(itripHotelOrder);
        ItripHotelOrder itripHotelOrder1 = itripHotelOrderMapper.selectByOrderNo(out_trade_no);
        ItripTradeEnds itripTradeEnds = new ItripTradeEnds();
        itripTradeEnds.setId(itripHotelOrder1.getId());
        itripTradeEnds.setOrderno(itripHotelOrder1.getOrderno());
        itripTradeEnds.setFlag((byte)0);
        //增加订单后续待处理记录
        itripTradeEndsMapper.insert(itripTradeEnds);
        //更新库存
        itripTradeEndsMapper.updateItripTradeEnds(1L, 0L);
        ItripHotelOrder itripHotelOrder2 = itripHotelOrderMapper.selectByOrderNo(itripHotelOrder1.getOrderno());
        Map<String, Object> roomStoreMap = new HashMap();
        roomStoreMap.put("startTime",itripHotelOrder2.getCheckindate());
        roomStoreMap.put("endTime",itripHotelOrder2.getCheckoutdate());
        roomStoreMap.put("count", itripHotelOrder2.getCount());
        roomStoreMap.put("roomId", itripHotelOrder2.getRoomid());
        itripHotelTempStoreMapper.updatetore(roomStoreMap);
        itripTradeEndsMapper.updateItripTradeEnds(2L, 1L);
    }

    /**
     * 支付失败后订单修改
     * @param out_trade_no
     * @param payType
     * @param trade_no
     * @return
     * @throws Exception
     */
    @Override
    public void payFailed(String out_trade_no, Integer payType, String trade_no) throws Exception {
        ItripHotelOrder itripHotelOrder=new ItripHotelOrder();
        itripHotelOrder.setOrderno(out_trade_no);
        itripHotelOrder.setOrderstatus(1);
        itripHotelOrder.setPaytype(payType);
        itripHotelOrder.setTradeno(trade_no);
        itripHotelOrderMapper.updateOrderByPay(itripHotelOrder);
    }

    /**
     * 判断该订单是否已经支付
     * @param out_trade_no
     * @return
     * @throws Exception
     */
    @Override
    public boolean processed(String out_trade_no) throws Exception {

        ItripHotelOrder itripHotelOrder=itripHotelOrderMapper.selectByOrderNo(out_trade_no);
        if(itripHotelOrder.getOrderstatus()==2&&itripHotelOrder.getTradeno().length()>0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
